package cz.muni.fi.pv112.cranesimulator.scene.handlers;

import cz.muni.fi.pv112.cranesimulator.models.scene.Container;
import cz.muni.fi.pv112.cranesimulator.scene.Scene;
import java.util.Random;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import static cz.muni.fi.pv112.cranesimulator.utils.handlers.opengl.ModelDisplayHandler.drawModelFromDisplaylist;
import static java.util.Calendar.getInstance;

/**
 *
 * @author Michal Ďuriš
 */
public class ContainerHandler {

    public static void initContainerModels(GL gl, Container[] containers) {
        for (Container container : containers) {
            initContainerModel(gl, container);
        }
    }

    public static void initContainerModel(GL gl, Container container) {
        Random randomGenerator = new Random(getInstance()
                .getTimeInMillis());
        container.init(gl);
        container.moveTo(0.0f,
                0.0f,
                40 + randomGenerator.nextInt(200));

        container.rotateTo(0.0f, randomGenerator.nextInt(360), 0.0f);
    }

    public static void displayContainers(GL2 gl, Container[] containers) {
        for (Container container : containers) {
            displayContainer(gl, container);
        }
    }

    public static void displayContainer(GL2 gl, Container container) {
        gl.glPushMatrix();

        gl.glRotatef(-container.getRotation()[Scene.Y_AXIS], 0.0f, 1.0f, 0.0f);

        gl.glTranslatef(container.getDisplacement()[Scene.X_AXIS],
                container.getDisplacement()[Scene.Y_AXIS],
                container.getDisplacement()[Scene.Z_AXIS]);

        drawModelFromDisplaylist(gl, container);
        gl.glPopMatrix();
    }

}
